/*
 * DbConnectionPool.h
 *
 *  Created on: 2018年1月3日
 *      Author: chuanjiang.zh@qq.com
 */

#ifndef DBCONNECTIONPOOL_H_
#define DBCONNECTIONPOOL_H_

#include "DbSource.h"
#include <memory>
#include <mutex>
#include <atomic>

#include "DbConnection.h"


namespace db
{

class DbConnectionPool
{
public:
    
public:
	explicit DbConnectionPool(size_t maxSize = 3);
	virtual ~DbConnectionPool();

	bool open(const DbSource& source);
	void close();

	bool isOpen();

	size_t maxSize() const;

	soci::connection_pool& getPool();

	const std::string& getBackend();

	const std::string& getConnectionStr();

    bool reconnect();

    std::mutex& getLock();

    bool isSqlite() const;

    const DbSource& getSource() const;

	bool reopen();

protected:
    size_t	m_maxSize;

	soci::connection_pool* m_pool;

	DbSource	m_source;
	std::string m_backend;
	std::string m_connectString;

    std::atomic_bool m_opened;

    std::mutex  m_mutex;

};


typedef std::shared_ptr< DbConnectionPool >     DbConnectionPoolPtr;



} /* namespace db */


#endif /* DBCONNECTIONPOOL_H_ */
